#pragma once
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv2/core/core.hpp>
#include <cstring>
#include <vector>
#include "RollingGuidanceFilter.h"

using namespace cv;

// the fish _tail position in the marimage .
enum TailPosition{
	LEFTTOP,
	LEFTBOTTOM,
	RIGHTTOP,
	RIGHTBOTTOM,
	LEFTCENTER,
	RIGHTCENTER,
	NONE
};

class DrawAttribute
{
public:
	DrawAttribute(IplImage* marImg);
	DrawAttribute();
	void saveMarginalPoints(std::string filename="./01.txt");
	unsigned int getLength();
	unsigned int getWidth(); 
	unsigned int getCrossLength(); 
	unsigned int getEyeCrossLength(); 
	IplImage* getMarImage(){ return _marImage;}
	IplImage* getRawImage(){ return _rawImage;}
	void setRawImage(IplImage* image);
	Point2i getHeadPoint() {return _head;}
	Point2i getTailPoint() {return _tail;}
	Point2i getBackPoint(){ return _back;}
	Point2i getBellyPoint(){ return _belly;}
	Point2i getEyePoint(){ return _eye;}
	bool extractAttributeAlgorithm();
	Point2i getCrossPoint() {return _crossPoint;}
	bool drawingOutline(IplImage* rawImg=0,IplImage* marImg=0,int iteratorCount = 5,int thresh=40);
	void drawingOutlineWithInternal(IplImage* rawImg=0,IplImage* marImg=0,int thresh=50);
	~DrawAttribute(void);

private:
	Point2i _head,_tail,_back,_belly;
	Point2i _eye,_crossPoint;
	int _minw,_maxw;
	IplImage* _marImage;
	IplImage* _rawImage;
	std::vector<Point2i> _points;
	bool _isRotation;

private:
	void extractMarginalPoints(IplImage *marImage);
	void isolatePointFilter(IplImage* image,int sampleW=3,int sampleH=3,int threshold=5);
	int sampleCounter(IplImage* image,int currentX, int currentY, \
		int sampleW,int sampleH);
	Point2i iteratorApproximation(IplImage* marImage,Point2i initPoint,\
		TailPosition findDirection,int iterWidth = 15);
	Point2i calLeftTopCrossPoint(IplImage* marImage,Point2i initState,int width = 15);
	Point2i calLeftBottomCrossPoint(IplImage* marImage,Point2i initState,int width = 15);
	Point2i calRightTopCrossPoint(IplImage* marImage,Point2i initState,int width = 15);
	Point2i calRightBottomCrossPoint(IplImage* marImage,Point2i initState,int width = 15);
	Point2i calLeftCenterCrossPoint(IplImage* marImage,Point2i initState,int width = 15);
	Point2i calRightCenterCrossPoint(IplImage* marImage,Point2i initState,int width = 15);
	int calculateLengthAndWidth();
	void rotationImage(CvArr* src,CvArr *dst,float angle,Point2i center,Point2i offeset=cvPoint(0,0));
	void rotationPoint(Point2i src,Point2i &dst,float angle,Point2i center,Point2i offset=cvPoint(0,0));
	TailPosition calTailPosition(IplImage* src,Point2i head);
	float calAngle(Point2i vec1,Point2i vec2);

	bool findHeadAndTailPosition(IplImage* marImage,Point2i eye,Point2i &head,Point2i &tail);
	bool findBackAndBellyPosition(IplImage* marImage,Point2i &back,Point2i &belly,Point2i center,int width=50);
	Point2i findCenterInitPoint(IplImage* marImage,Point2i head);
	Point2i DrawAttribute::findCrossPoint(IplImage* marImage,Point2i head,\
		Point2i tail,int iterTimes = 5);
	Point2i findEyePosition(IplImage* image);
	bool DoAlgrithm();
};

